# _*_ coding: utf-8 _*_
#!/usr/bin/env python

import sys, os
import xml.sax

class XMLHander(xml.sax.ContentHandler):
	def __init__(self, tilemap):
		self.tilemap		= tilemap
		self.active_tag		= None
		self.row=0
	
	def startDocument(self):
		self.active_tag		= None

	def endDocument(self):
		return self.tilemap

	def startElement(self, name, attrs):
		self.row+=1
		self.active_tag		= None
		if name == 'tilerow':
			self.tilemap.append([])
			self.active_tag	= 'tilerow'
		if name == 'tile':
			self.active_tag	= 'tile'
			
	def endElement(self,name):
		#self.row+=1
		self.active_tag	= None
		#if name == "tilerow":
			#self.tilemap.append(self.tilerow)

	def characters(self,data):
		if self.active_tag == 'tile':
			try:
				self.tilemap[-1].append(int(data)) #int(data, 16))
			except:
				print data, self.row
				sys.exit()

	def ignorableWhitespace(self):
		print 'ignorableWhitespace'

tilemap = []
xml.sax.parse(os.path.join(os.path.dirname(sys.argv[0]), 'Mario_Circuit_1.map.xml'), XMLHander(tilemap))
#print len(tilemap)
##print tilemap[0]
#new_tilemap = []
#for row in tilemap:
	#for tile in row:
		#new_tilemap.append(int(tile, 16))
#self.tilemap